﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

// ngdlong - Use LERes
using LevelEditor.LERes;
using LevelEditor.ImageProcess;


using LevelEditor.Pattern;
// ngdlong - Use to load images

using System.IO;
using System.Collections;

// End load images

namespace LevelEditor
{
    public partial class LevelEditor : Form
    {
        Image2Tile imgProcesser = new Image2Tile();
        private PatternManager patrnObj;
        public static int m_iTileSize { get; set; } // size of each tile
        public static int m_iMapWidth { get; set; } // number of col 
        public static int m_iMapHeight { get; set; } // number of row
        private PictureBox[] pbList;
        private TableLayoutPanel gridViewObj;


        public LevelEditor()
        {
            InitializeComponent();
            patrnObj = PatternManager.Instance;
            patrnObj.LoadImage();
            
        }

        private void LevelEditor_Load(object sender, EventArgs e)
        {
            // Open level editor 

            // Do nothing right now

            // We should load all res from previous state for user
        }

        /// <summary>
        /// Click new map 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void newMapToolStripMenuItem_Click(object sender, EventArgs e)
        {

            // Show new map dialog
            NewMapUI frmNewMap = new NewMapUI(this);
            frmNewMap.Show();
            
        }

        private void quitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // Currently, we exit application
            Application.Exit();
        }

        public void GetData(int tileSize, int mapWidth, int mapHeight)
        {
            // Paste data
            m_iTileSize = tileSize;
            m_iMapWidth = mapWidth;
            m_iMapHeight = mapHeight;

            // Gen map
            GenNewMap(); 
        }

        /// <summary>
        ///  Gen new map with data
        ///  Gen UI using code
        /// </summary>
        private void GenNewMap()
        {


            // gen fake grid here

            gridViewObj = new TableLayoutPanel();
            gridViewObj.ColumnCount = m_iMapWidth;

            gridViewObj.Location = new Point(0, 0);
            gridViewObj.Name = "Fake grid";
            gridViewObj.RowCount = m_iMapHeight;

            // gen pic list here


        }




        /*
         * Pattern menu
         * 
         * */

        private Stream imageStream = null;
        

        private void loadImageToolStripMenuItem_Click(object sender, EventArgs e)
        {

            // Open file dialog, we need to load an image first before pre-process
            ofLoadImage.Filter = "Images (*.jpg)(*.png)|*.jpg, *.png, *.JPG, *.PNG|All files (*.*)|*.*";
            ofLoadImage.FilterIndex = 2;
            ofLoadImage.RestoreDirectory = true;

            

            if (ofLoadImage.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    string sImagePath = ofLoadImage.FileName;
                    if (sImagePath != null)
                    {
                            // Pre-process image 

                        imgProcesser.SplitImage(sImagePath);
                            //Requirement
                            // 1. User can select the select tile
                            // 2. Save as pattern


                            // Current issues
                            // 1. What should we do with image before post ?
                            // 2. What type of panel can handle such task ?
                    }
                }
                catch (Exception ex)
                {
                    // print ex
                    MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
                }
            }


            // We need to lock the current UI before load pattern UI
            this.Enabled = false;
           
            /*
             * temp remove this code
            // Load the pattern UI
            PatternUI patternFrm = new PatternUI();
            patternFrm.Show();
            patternFrm.Focus();
            */

        }

        /*
         * End pattern menu
         * */
        
    }
}
